<?php

require_once "conf/routes.php";

function route_for_request_path($path)
{
    $parsedString = explode("/",$path);
    $nbArgs = count($parsedString);
    
    /*if($nbArgs < 3){
		echo "Args NB problem !";
		exit -1;
	}*/
    
    if( isset($parsedString[2]) ){
	    $idString = $parsedString[2];
	    for($i = 3; $i < $nbArgs; $i++)
	    {
			$idString = $idString."/".$parsedString[$i];
		}
	} else {
		$idString = "";
	}
    
    $returnValue = array("controller" => $parsedString[0], "action" => $parsedString[1], "id" => $idString);
    return($returnValue);
}

function __autoload($className)
{
	$lgController = strlen( "Controller" );
	$fileName = strtolower( substr( $className, 0, strlen($className) - $lgController ) );	
	require_once "app/controllers/{$fileName}/{$fileName}.php";
}


function route_to($route)
{
	//créée une variable contenant ( si controller = saMplE    =>    SampleController )
	$className = ucfirst( strtolower( $route["controller"] ) )."Controller";
	$object = new $className();
	//$object->$route["action"]($route["id"]);
	$object->$route["action"]($route);

}

?>
